/**
 * FractalAplication.java
 * @author: liuex
 * 2008-1-6 ����05:14:11
 *
 * note: 
 */
package lex.fractal.swing.common;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;

import org.lex.swing.ActionBuilder;
import org.lex.swing.widgets.EnhancedDialog;

import lex.fractal.algorithm.Fractal;

public class FractalFrame extends JFrame
{
	private Fractal fractal = null;
	private FractalAnimator animator = null;
	private JPopupMenu popup = null;

	public FractalFrame( Fractal fractal )
	{
		super();
		this.fractal = fractal;
		this.initialize();
	}

	private void initialize()
	{
		this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
		this.setSize( 800, 600 );
		this.setTitle( fractal.getName() );
		this.setContentPane( this.getFractalAnimator() );
	}

	private FractalAnimator getFractalAnimator()
	{
		if( null == animator )
		{
			animator = new FractalAnimator( fractal );
			animator.setComponentPopupMenu( this.getPopup() );
		}
		return animator;
	}

	private JPopupMenu getPopup()
	{
		if( null == popup )
		{
			popup = new JPopupMenu();
			popup.add( new ControlPaneAction() );
		}
		return popup;
	}

	class ControlPaneAction extends ActionBuilder
	{
		ControlPaneAction()
		{
			this.setName( "Control Pane" );
			this.setMnemonic( 'C' );
			setAcceleratorKey( KeyStroke.getKeyStroke( KeyEvent.VK_P, KeyEvent.ALT_DOWN_MASK ) );
		}
		@Override
		public void actionPerformed( ActionEvent e )
		{
			EnhancedDialog d = new EnhancedDialog( FractalFrame.this );
			d.setTitle( fractal.getName() );
			d.setContentPane( new AnimatorControlPane( getFractalAnimator() ) );
			d.pack();
			d.setLocationRelativeTo( FractalFrame.this );
			d.setVisible( true );
		}
		
		
	}
}
